﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SCOSub
{
    public partial class Export : Form
    {
        private WorkMode workmode;
        private string MainRichText;
        public delegate void SetMainFormTopMostHandle(bool topmost);
        public event SetMainFormTopMostHandle SetMainFormTopMost;//设置MainForm的TopMost
        private bool hasChooseSavePath = false;

        public Export(string MainRichText, WorkMode workmode, string savepath)
        {
            InitializeComponent();
            this.workmode = workmode;
            this.MainRichText = MainRichText;
            this.TopMost = Const.DoTopMost;

            switch (workmode)
            {
                case WorkMode.Staff: userControl_Staff1.Visible = true; break;
                case WorkMode.Time: userControl_Time1.Visible = true; break;
                case WorkMode.Effect: userControl_Effect1.Visible = true; break;
            }
            if (savepath != string.Empty)
                SaveFilePath_textBox.Text = Path.ChangeExtension(savepath, ".ass");
        }
        private void Export_FormClosed(object sender, FormClosedEventArgs e)
        {
            SetMainFormTopMost(true);
        }

        private void SavePathbutton_Click(object sender, EventArgs e)
        { 
            saveFileDialog1.Filter = Errors.Export_SaveFileDialogFilter;
            saveFileDialog1.ShowDialog();
        }

        private void Save_button_Click(object sender, EventArgs e)
        {
            savefile(SaveFilePath_textBox.Text, richTextBox1.Text);
        }

        private bool savefile(string path, string note)
        {
            StreamWriter sw;
            StringReader sr = new StringReader(note);
             
            try
            {
                sw = new StreamWriter(path, false, Encode_checkBox.Checked ? Encoding.Unicode : Encoding.Default);
            } 
            catch(Exception ex)
            {
                MessageBox.Show(Errors.Er_Write + "\n" + ex.ToString(), Errors.Er_Write_Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            /*有问题if (!hasChooseSavePath)
            {
                MessageBox.Show(Errors.Er_Write, Errors.Er_Write_Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                sw.Dispose();
                return false;
            }*/
            while (sr.Peek() > -1)
            {
                sw.WriteLine(sr.ReadLine());
            }
            sw.Dispose();

            return true;
        }

        private void Build_button_Click(object sender, EventArgs e)
        {
            Save_button.Enabled = true;
            switch (workmode)
            {
                case WorkMode.Staff:
                    {
                        IStaffModeBuildable ism = new ChangeFormat_Staff_Mode();
                        if (userControl_Staff1.CFR_radioButton.Checked)
                            richTextBox1.Text = ism.Run(MainRichText, (double)userControl_Staff1.fps_numeric.Value);
                        else
                        {
                            if (userControl_Staff1.tc == null)
                                richTextBox1.Text = ism.Run(MainRichText, userControl_Staff1.tcpath_textbox.Text);
                            else richTextBox1.Text = ism.Run(MainRichText, userControl_Staff1.tc);
                        }
                        break;
                    }
                case WorkMode.Time:
                    {
                        ITimeModeBuildable itm = new ChangeFormat_Time_Mode();
                        richTextBox1.Text = itm.Run(MainRichText, userControl_Time1);
                        break;
                    }
                case WorkMode.Effect:
                    {
                        IEffectModeBuildable iem = new ChangeFormatEffects();

                        richTextBox1.Text = iem.effectbuild(MainRichText, userControl_Effect1.EffectStyle_combobox.Text.Trim());//转入后台
                        break;
                    }
            }
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            SaveFilePath_textBox.Text = saveFileDialog1.FileName;
            hasChooseSavePath = true;
        }

        

       

    }
   
}
